/*
 * @Title: 我的订单
 * @port: 后台开发人员
 * @Author: 徐兵
 * @Date:   2018-10-08 11:45:10（写此页面的日期）
 * @Last Modified by:   Administrator
 * @Last Modified time: 2018-11-15 11:17:12
 */
var app = {},
  dataInfo = new Object(),
  module = {
    init: function() {
      app = new Vue({
        el: '#app',
        data: {
          radio: is_weixn() ? '4' : '5', //4 - wx , 5 - zfb
          isWx: is_weixn(),
          show: false,
          count: 0,
          isLoading: false,
          topTab: {
            active: 0,
            title: ["全部", "待付款", "待发货", "待收货", "已完成"],
          },
          orderGetSet: {
            userId: userId,
            orderStatus: "",
            page: 1,
            limit: 10000,
          },
          userId: userId,
          orderId: "",
          orderList: [],
          aliPay: "",
          isPt: 0, //当前付款商品是否拼团判断 1是 0否
        },
        methods: {
          init: function() {
            app.orderGetSet.orderStatus = getQueryString("orderStatus");
            app.topTab.active = getQueryString("active");
            app.orderGet();
          },
          pay: function(orderId, num) { //待付款状态跳转付款 - 弹窗
            app.orderId = orderId;
            app.show = true;
            this.isPt = 1;
          },
          close: function() {
            app.show = false;
          },
          l: function(leng) { //共计多少件
            return leng.length;
          },
          goApp: function() {
            app.$dialog.confirm({
              title: '提示',
              message: '请下载姿美汇APP进行申请退款'
            }).then(() => {
              // on confirm
              downAppFun();
            }).catch(() => {
              // on cancel
            });
          },
          mounted: function(orderId) { //删除订单
            this.$dialog.confirm({
              message: '确认删除订单吗？'
            }).then(() => { //确认
              console.log("确认")
              app.$toast.loading({
                forbidClick: true,
                loadingType: 'spinner',
                duration: 0,
              });
              ajaxTemplate(addressUrl + SERVICE.deleteOrder, function(data) {
                app.$toast.clear()
                if (data.tag == 0) {
                  app.orderGet();
                } else {
                  app.$toast(data.errMsg)
                };
              }, {
                userId: userId,
                orderId: orderId
              });

            }).catch(() => { //取消
              console.log("取消")
            });
          },
          confirmOrder: function(orderId) { //确认收货
            app.$toast.loading({
              forbidClick: true,
              loadingType: 'spinner',
              duration: 0,
            });
            ajaxTemplate(addressUrl + SERVICE.confirmOrder, function(data) {
              app.$toast.clear()
              if (data.tag == 0) {
                app.orderGet();
              } else {
                app.$toast(data.errMsg)
              };
            }, {

              userId: userId,
              orderId: orderId
            });

          },

          cancelOrder: function(orderId) { //取消订单
            this.$dialog.confirm({
              message: '确认取消订单吗？'
            }).then(() => { //确认

              app.$toast.loading({
                forbidClick: true,
                loadingType: 'spinner',
                duration: 0,
              });
              ajaxTemplate(addressUrl + SERVICE.cancelOrder, function(data) {
                app.$toast.clear()
                if (data.tag == 0) {
                  app.orderGet();
                } else {
                  app.$toast(data.errMsg)
                };
              }, {

                userId: userId,
                orderId: orderId
              });

            }).catch(() => { //取消
              console.log("取消")
            });
          },


          onRefresh: function() { //下拉刷新
            app.orderGet();
            setTimeout(() => {
              /* this.$toast('刷新成功');*/
              this.isLoading = false;
              this.count++;
            }, 200);
          },
          onClick: function(index, title) { //tab
            $(".vue-loading").show()
            if (index == 0) {
              app.orderGetSet.orderStatus = 0
            } else if (index == 1) {
              app.orderGetSet.orderStatus = 10
            } else if (index == 2) {
              app.orderGetSet.orderStatus = 20
            } else if (index == 3) {
              app.orderGetSet.orderStatus = 22
            } else if (index == 4) {
              app.orderGetSet.orderStatus = 30
            }
            app.orderGet();
          },
          orderGet: function() { //获取列表数据
            ajaxTemplate(addressUrl + SERVICE.orderGet, function(data) {
              if (data.tag == 0) {
                app.orderList = data.data.result;
                $(".vue-loading").hide()
              } else {
                app.$toast(data.errMsg)
              };
            }, app.orderGetSet);
          },
          toPay: function() {
            app.$toast.loading({
              forbidClick: true,
              message: '数据提交中...',
              loadingType: 'spinner',
              duration: 0,
            });
            app.show = false;
            app.payInfo = {}
            app.payInfo.orderId = app.orderId;
            app.payInfo.ptType = 'fx';
            app.radio == '4' ? app.payInfo.channel = 'h5fxcwx' : app.payInfo.channel = 'h5fxcali'
            app.radio == '4' ? app.payInfo.openid = openId : app.payInfo.returnUrl = window.location.href.split('/html/')[0] + '/html/orderDetails.html?orderId=' + this.orderId + '&tag=' + 1;
            if (!!this.isWx && (window.location.href.indexOf('mtmall.meetao.com') != 8)) { //微信支付跳转第三方
              //版本号1.1 没有拼团数据
              //1 - 普通支付（分销商品、大礼包）、 2 - 普通订单支付 、3 - 拼团支付 、 4 - 拼团订单支付
              this.isPt == 1 ? dataInfo.payType = 4 : dataInfo.payType = 2;
              location.href = 'https://premtmall.meetao.com/html/WxPay.html?mallUrl=' + window.location.href.split('/html/')[0] + '&ajaxUrl=' + addressPayUrl + '&orderId=' + this.orderId + '&openid=' + app.payInfo.openid + '&payType=' + dataInfo.payType;
            } else {
              ajaxTemplate(addressPayUrl + SERVICE.payUrl, function(data) {
                /* app.$toast.clear();*/
                if (data.tag == 0 && app.radio == '4') { //wxPay
                  if (typeof WeixinJSBridge == "undefined") {
                    if (document.addEventListener) {
                      document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                    } else if (document.attachEvent) {
                      document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                      document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                    }
                  } else {
                    app.$toast.clear()
                    app.onBridgeReady(data.data.result);
                  }

                } else if (data.tag == 0 && app.radio == '5') { //alipay
                  if (getQueryString("tag") == 1) {
                    app.getOrderInfo()
                  }
                  app.aliPay = decodeURIComponent(data.data.result.sign.replace(/\+/g, " "));
                  setTimeout(function() {
                    document.forms[0].submit();
                  }, 250);
                } else {
                  app.$toast.clear();
                  app.$toast(data.errMsg);
                };

              }, app.payInfo);
            };
          },
          onBridgeReady: function(msg) {
            WeixinJSBridge.invoke(
              'getBrandWCPayRequest', {
                "appId": msg.appId, //公众号名称，由商户传入
                "timeStamp": msg.timeStamp, //时间戳，自1970年以来的秒数
                "nonceStr": msg.nonceStr, //随机串
                "package": msg.package_str,
                "signType": "MD5", //微信签名方式：
                "paySign": msg.paySign //微信签名
              },
              function(res) {
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                  app.getOrderInfo()
                  /*app.getOrderDetail() //订单详情*/
                  window.location.reload()
                } else {
                  window.location.reload()
                }
              });
          },
          getOrderInfo: function() { //获取订单数据
            ajaxTemplate(addressUrl + SERVICE.getOrderInfo, function(data) {
              log(data);
              if (data.tag == 0) {
                //	dataInfo.init.orderInfo = data.data.result[0];
              } else {
                dataInfo.init.$toast(data.errMsg);
              };
            }, {
              userId: userId,
              orderId: app.orderId
            });
          },
        }
      });
      app.init()
    }
  }
module.init()
